package com.linkq.ipinyin.controller;

import com.linkq.ipinyin.model.Order;
import com.linkq.ipinyin.service.CustomerService;
import com.linkq.ipinyin.service.OrderService;
import com.linkq.ipinyin.utils.IdUtil;
import com.linkq.ipinyin.vo.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author wangpeng
 * @since 2025-06-29
 **/

@RestController
@RequestMapping("/pay/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private CustomerService customerService;

    /**
     * 创建订单
     * @param order 订单信息
     * @return
     */
    @PostMapping("/new")
    public ResultInfo newOrder(@RequestBody Order order) {
        int userId = customerService.getUser().getId();
        order.setId(IdUtil.getUUID());
        order.setStatus(1);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setUserId(customerService.getUser().getId());
        if (orderService.insert(order) > 0) {
            return ResultInfo.success(order);
        } else {
            return ResultInfo.error("创建订单失败");
        }
    }

    /**
     * 查询订单
     * @param id id
     * @return
     */
    @GetMapping("/query/{id}")
    public ResultInfo queryOrderById(@PathVariable(value = "id") Integer id) {
        Order order = orderService.selectById(id);
        if (order != null) {
            return ResultInfo.success(order);
        } else {
            return ResultInfo.error("订单不存在");
        }
    }
}
